Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

r - Creating dataframe under reactive values

Not sure what is wrong with the below code, when I try to change the changes the values in "Change to" section the table is not getting updated.Is there way to change the table as we changes the text in "Change to"? Can anyone guide me on this?

library(shiny)
library(DT)

ui <- navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(
             sidebarPanel(
               uiOutput("tex2"),
               uiOutput("book3"),
               uiOutput("book6")
             ),
             mainPanel(
               DT::dataTableOutput("hot3")
             )
           )))
#server.r
server <- function(input, output,session) {
  
  
  
  output$tex2<-renderUI({
    numericInput("text2", "#tests", value = 1, min=1)
  })
  
  output$book3<-renderUI({
    selectInput("bk3", "Label",  choices=(paste("Test",1:input$text2)))
  })
  
  output$book6<-renderUI({
    textInput("bk6", "Change to", value=NULL)
  })
  
  df_reactive <- reactiveValues()
  
  rt4 <- reactive({
    df_reactive$DF <- data.frame(
      Test=paste(1:input$text2),
      Label=paste("Test",1:input$text2),
      stringsAsFactors = FALSE)
    if(!is.null(input$bk6) && input$bk6!=""){
      df_reactive$DF[df_reactive$DF$Label==isolate(input$bk3), "Label"] <- input$bk6
    }
    {
      df_reactive$DF
    }
    
  })
  
  output$hot3 <-DT::renderDataTable(
    rt4(),
    rownames= FALSE
    
  )
  
}
shinyApp(ui, server)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This was a bit unclear to me, but I think the problem has to do with changing your reactiveValues within the reactive expression.

It would seem that setting df_reactive$DF the first time, will trigger the rt4(), as well as when it is called again in changing "Label". It would seem rt4() would run over and over repeatedly.

Try using a temporary variable to develop your data.frame the way you'd like, and then set df_reactive$DF in the end (and returned in rt4()):

  rt4 <- reactive({
    df <- data.frame(
      Test=paste(1:input$text2),
      Label=paste("Test",1:input$text2),
      stringsAsFactors = FALSE)
    if(!is.null(input$bk6) && input$bk6!=""){
      df[df$Label==isolate(input$bk3), "Label"] <- input$bk6
    } else {
      df
    }
    df_reactive$DF <- df
  })

Note: depending on your goals, you may want to use eventReactive and make other modifications (do you need df_reactive$DF at all?), so that changes are only made in certain conditions (such as input changes) - and not your reactiveValues (or use isolate).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...