简体   繁体   English

反应性数据框中的闪亮-重命名因素

[英]Shiny - renaming factors in reactive data frame

I'm building a shiny app that queries an SQL database so the user can ggplot the data. 我正在构建一个查询SQL数据库的闪亮应用程序,以便用户可以对数据进行ggplot。 I would like the user to be able to rename factors manually but am struggling to get going. 我希望用户能够手动重命名因子,但正在努力前进。 Here is an example of what I want to do: 这是我想做的一个例子:

ui.R ui.R

library(markdown)

shinyUI(fluidPage(
  titlePanel("Reactive factor label"),
  sidebarLayout(
    sidebarPanel(

      numericInput("wafer", label = h3("Input wafer ID:"), value = NULL),

      actionButton("do", "Search wafer"),
      textInput("text", label = h3("Factor name to change"), value = ""),
      textInput("text", label = h3("New factor name"), value = ""),
      actionButton("do2", "Change name")

       ),

    mainPanel(
      verbatimTextOutput("waf"), 
      verbatimTextOutput("que"), 
      verbatimTextOutput("pos"),
      dataTableOutput(outputId="tstat")
    )
  )
)
)

server.R server.R

# Create example data

Name <- factor(c("Happy", "New", "Year"))
Id <- 1:3

dd <- data.frame(Id, Name)

con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "dd", dd)
query <-  function(...) dbGetQuery(con, ...)

wq = data.frame()
sq = data.frame()


shinyServer(function(input, output){

  # create data frame to store reactive data set from query
  values <- reactiveValues()
  values$df <- data.frame()

  # Wait for user to search
  d <- eventReactive(input$do, { input$wafer })

  # Reactive SQL query
  a <- reactive({ paste0("Select * from dd where Id=",d()) })
  wq <- reactive({  query( a() ) })

  # Check outputs
  output$waf <- renderPrint(input$wafer)
  output$que <- renderPrint({ a() }) 
  output$pos <- renderPrint( wq()[1,1] )

  # observe d() so that data is not added until user presses action button  
  observe({
    if (!is.null(d())) {
      sq <- reactive({  query( a() ) })

      # add query to reactive data frame
      values$df <- rbind(isolate(values$df), sq())
    }
  })

  output$tstat <- renderDataTable({
    data <- values$df
  })

})

In static RI would normally use data table to rename factors ie: 在静态RI中,通常将使用数据表来重命名因子,即:

DT <- data.table(df)
DT[Name=="Happy", Name:="Sad"]

But I'm not sure how to go about this with a reactiveValues ie values$df . 但是我不确定如何使用reactValues即values$df

I have read this ( R shiny: How to get an reactive data frame updated each time pressing an actionButton without creating a new reactive data frame? ). 我读过这篇文章( R闪亮:如何在每次不创建新的反应式数据框架的情况下每次按下actionButton来更新反应式数据框架? )。 This lead me to try this but it doesn't do anything (even no error): 这使我尝试这样做,但是它什么也没做(即使没有错误):

observeEvent(input$do2, {
    DT <- data.table(values$df) 
    DT[Name == input$text1, Name := input$text2]
    values$df <- data.frame(values$df)

  })

Perhaps there is a way around this..maybe there is a way to use an action button to "lock in" the data as a new data frame, which can then be used to rename? 也许有办法解决。.也许有办法使用操作按钮将数据“锁定”为新的数据框,然后可以使用它重命名?

Sorry for such a long winded question. 抱歉,这么长的问题。 My real app is much longer and more complex. 我的真实应用程序更长,更复杂。 I have tried to strip it down. 我试图将其剥离。

Your approach works but there are a few issues in your app. 您的方法可行,但是您的应用程序中存在一些问题。

In ui.R , both textInput have the same id , they need to be different so you can refer to them in the server.R . ui.R ,两个textInput具有相同的id ,它们需要不同,因此您可以在server.R引用它们。 In the observeEvent you posted, you refer to input$text1 and input$text2 so you should change the id of the textInputs to text1 and text2 . 在发布的observeEvent ,您引用了input$text1input$text2因此应将textInputsid更改为text1text2

In the observeEvent you posted, the last line should be values$df <- as.data.frame(DT) , otherwise it does not change anything. 在您发布的observeEvent ,最后一行应为values$df <- as.data.frame(DT) ,否则它不会改变任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM