首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑反应性数据库

编辑反应性数据库
EN

Stack Overflow用户
提问于 2021-12-23 23:59:50
回答 1查看 67关注 0票数 1

尝试编辑反应性数据库,以便在输出中反映对数据库的更新。已经尝试了许多变体,但没有工作,一般的想法是显示-在这里,我希望有更改的数据库图形更新。

代码语言:javascript
复制
library(tidyverse)
library(shiny)
library(DT)

# Define UI for application that draws a histogram
ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            sliderInput("ages", "Max age:", 10, 100, 15),
            sliderInput("nsamp",
                        "Sample size:",
                        min = 10,
                        max = 1000,
                        value = 100)),
        
        mainPanel(dt_output('Sample sizes and weighting', 'x1'),
                  plotOutput("fig"))
    )
)

server <- function(input, output) {
   
    x = reactive({
        df = data.frame(age = 1:input$ages,
                        samples = input$nsamp,
                        weighting = 1)
    })

    output$x1 = renderDT(x(), 
                         selection = 'none', 
                         editable = TRUE,
                         server = TRUE,
                         rownames = FALSE)

    output$fig =  renderPlot({
        ggplot(x(), aes(age, samples)) +
          geom_line() +
          geom_point()
    })
    
}

shinyApp(ui = ui, server = server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-24 02:32:40

我们可以使用input$x1_cell_editreactiveValues来修改传递给绘图的数据。

注意isolaterenderDT中的使用,即在修改db$database时防止表重新呈现。

代码语言:javascript
复制
library(tidyverse)
library(shiny)
library(DT)

# Define UI for application that draws a histogram
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("ages", "Max age:", 10, 100, 15),
      sliderInput("nsamp",
        "Sample size:",
        min = 10,
        max = 1000,
        value = 100
      )
    ),
    mainPanel(
      dataTableOutput("x1"),
      plotOutput("fig")
    )
  )
)

server <- function(input, output) {

  # all the data will be stored in this two objects
  db <- reactiveValues(database = NULL)

  # to store the modified values
  edited_vals <- reactiveVal(tibble(row = numeric(), col = numeric(), value = numeric()))

  # create a new table each time the sliders are changed
  observeEvent(c(input$ages, input$nsamp), {
    df <- data.frame(
      age = 1:input$ages,
      samples = input$nsamp,
      weighting = 1
    )
    db$database <- df
  })



  observeEvent(input$x1_cell_edit, {
    db$database[as.numeric(input$x1_cell_edit$row), as.numeric(input$x1_cell_edit$col + 1)] <- as.numeric(input$x1_cell_edit$value)
  })

  output$x1 <- renderDT(
    {
      input$ages
      input$nsamp

      datatable(
        isolate(db$database),
        selection = "none",
        editable = TRUE,
        rownames = FALSE,
        options = list(stateSave = TRUE)
      )
    },
    server = TRUE
  )

  output$fig <- renderPlot({
    ggplot(db$database, aes(as.numeric(age), as.numeric(samples))) +
      geom_point() +
      geom_line()
  })
}

shinyApp(ui = ui, server = server)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70468440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档