2017-08-30 5 views
1

ベクトル値で指定された列を埋めボタン(A Col1に導入された値の変換、例えば、Col1の各行に乱数を掛ける)。 どのように私は、次のサンプルコードにボタンを追加する必要がありますrhandsontableシャイニーアプリケーション:私は、ユーザーが番号を1列(例えばのCol1)を記入して、col2のエントリを生成するためのボタンを押すことが可能な光沢のあるアプリケーションを準備したいと思います

library(rhandsontable) 
library(shiny) 
# add a sparkline chart 
DF$chart = sapply(1:10, function(x) jsonlite::toJSON(list(values=rnorm(10)))) 
rhandsontable(DF, rowHeaders = NULL) %>% 
    hot_col("chart", renderer = htmlwidgets::JS("renderSparkline")) 

editTable <- function(DF, outdir=getwd(), outfilename="table"){ 
    ui <- shinyUI(fluidPage(
    titlePanel("Edit and save a table"), 
    sidebarLayout(
     sidebarPanel(
     helpText("Shiny app based on an example given in the rhandsontable package.", 
       "Right-click on the table to delete/insert rows.", 
       "Double-click on a cell to edit"), 

wellPanel(
      h3("Table options"), 
      radioButtons("useType", "Use Data Types", c("TRUE", "FALSE")) 
     ), 
     br(), 

wellPanel(
      h3("Save"), 
      actionButton("save", "Save table") 
     ),  

     wellPanel(
      textOutput('result') 
     ) 
    ), 

     mainPanel(
     rHandsontableOutput("hot") 
    ) 
    ) 
    )) 
    server <- shinyServer(function(input, output) { 

    values <- reactiveValues() 

    ## Handsontable 
    observe({ 
     if (!is.null(input$hot)) { 
     DF = hot_to_r(input$hot) 
     } else { 
     if (is.null(values[["DF"]])) 
      DF <- DF 
     else 
      DF <- values[["DF"]] 
     } 
     values[["DF"]] <- DF 
    }) 

    output$hot <- renderRHandsontable({ 
     DF <- values[["DF"]] 
     if (!is.null(DF)) 
     rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all") 
    }) 

    ## Save 
    observeEvent(input$save, { 
     finalDF <- isolate(values[["DF"]]) 
     saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename))) 
    }) 

    }) 

    ## run app 
    runApp(list(ui=ui, server=server)) 
    function(input, output) 
    return(invisible()) 
} 

(DF <- data.frame(Col1=1:10, Col2=runif(10), stringsAsFactors = FALSE)) 

editTable(DF) 

答えて

0

あなたはそのような何かを探しています:

ui <- shinyUI(fluidPage(
    titlePanel("Edit and save a table"), 
    sidebarLayout(
     sidebarPanel(
     helpText("Shiny app based on an example given in the rhandsontable package.", 
       "Right-click on the table to delete/insert rows.", 
       "Double-click on a cell to edit"),   
     wellPanel(
      h3("Table options"), 
      radioButtons("useType", "Use Data Types", c("TRUE", "FALSE")), 
      actionButton("Calculate", "Calculate column 2") 
     ), 
     br(),   
     wellPanel(
      h3("Save"), 
      actionButton("save", "Save table") 
     ),    
     wellPanel(
      textOutput('result') 
     ) 
    ), 

     mainPanel(
     rHandsontableOutput("hot") 
    ) 
    ) 
)) 

    server <- shinyServer(function(input, output) {  
    #extract data from the table to be used 
    portfoliovals2 <- reactive({ 
     live_data = hot_to_r(input$hot)[,1] 
     return(live_data) 
    })  
    #extract data to be used 
    portfoliovalsB2 <- reactive({ 
     live_data = hot_to_r(input$hot)[,2] 
     return(live_data) 
    }) 


    ## create a new df every time the table is modified 
    new_df_g <- reactive({ 
     initial_dataEdit = portfoliovals2() 
     initial_dataEditB = portfoliovalsB2() 
     initial_dataEditW <- data.frame(initial_dataEdit, initial_dataEditB)  
     return(initial_dataEditW) 
    }) 

    output$hot <- renderRHandsontable({ 

     if (input$Calculate ==0){ 
     initial_data = data.frame(Col1=1:10, Col2=runif(10), stringsAsFactors = FALSE) 

     } else { 
     initial_data = new_df_g() 
     initial_data <- plyr::rename(initial_data, c("initial_dataEdit"="Col1", "initial_dataEditB"="Col2")) 

     initial_data$Col2 <- initial_data$Col1 * 1.3 
     } 

     rhandsontable(initial_data, readOnly = FALSE, rowHeaders = NULL, height = 600) %>% 
     hot_cols(columnSorting = TRUE) 
    }) 
    }) 

    ## Save 
    observeEvent(input$save, { 
     finalDF <- new_df_g() 
     saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename))) 
    }) 

    }) 

    ## run app 
    runApp(list(ui=ui, server=server)) 
関連する問題